#include "head.h"

void *threadMain(void *p){
    pool_t *pool = (pool_t *)p;
    
    int begin_length = 0;
    user_info info;
    //char *tempstr = (char *)calloc(0, 1024);

    memset(&info, 0, sizeof(info));
    //连接数据库
    MYSQL *conn = initSql(pool);
    //初始化火车
    train_t train;

    while(1){

        //printf("thread\n");
        int net_fd; 
        pthread_mutex_lock(&pool->lock);
        // 判断如果队列中, 没有net_fd, 陷入阻塞(条件变量)
        while(pool->queue.size <= 0 && pool->exitFlag  == 0){

            //printf("thread before wait\n");
            int ret_wait = pthread_cond_wait(&pool->cond, &pool->lock);
            THREAD_ERROR_CHECK(ret_wait, "pthread_cond_wait");
        }

        printf("thread after while\n");
        if(pool->exitFlag == 1){
            // 退出标记位是退出状态, 主线程要求子线程退出
            // ....处理要处理的事情

            pthread_mutex_unlock(&pool->lock);

            pthread_exit(NULL);
        }

        net_fd = pool->queue.head->net_fd;
        int user_id;

        deQueue(&pool->queue);
        //printf("thread before unlock\n");

        int ret_unlock = pthread_mutex_unlock(&pool->lock);
        THREAD_ERROR_CHECK(ret_unlock, "pthread_mutex_unlock");

        printf("sonthread\n");
        //判断客户端指令
        int thread_epoll_fd = epoll_create(1);
        addEpoll(thread_epoll_fd, net_fd);
        //存储服务端绝对路径,配置文件里的root
        char server_path[60] = {0};
        while(1) {
            printf("before wait\n");
            struct epoll_event events[5];
            int epoll_num = epoll_wait(thread_epoll_fd, events, 5, -1);
            memset(&info, 0, sizeof(info));
            //接收命令
            recvInfoStruct(net_fd, &info);

            printf("command_num=%d\n", info.command_num);
            if(info.command_num == 10) {
                break;
            }

            printf("num=%d\n", info.command_num);
            switch(info.command_num) {
            case 0:
                //登录
                LOGI("client connect,user_id = %s", info.username);
                if(info.flag == 1) {
                    userLogin(net_fd, &info, conn, pool);
                }
                else if(info.flag ==0) {
                    userRegister(net_fd, &info, conn, pool);
                }
                printf("%s\n",info.path);
                break;
            case 1:
                //cd
                LOGI("user:%s, cd\n", info.username);
                info.flag =  fun_cd(conn, info.username, info.command_str_goto, info.path, pool);
                sendInfoStruct(net_fd, &info);
                break;
            case 2:
                //ls
                LOGI("user:%s, ls\n", info.username);

                fun_ls_by_sql(conn, &info, &train);
                memset(info.command_str_return, 0, sizeof(info.command_str_return));
                sprintf(info.command_str_return, "%s", train.buf);
                sendInfoStruct(net_fd, &info);
                break;
            case 4:
                LOGI("user:%s, ls\n", info.username);
                memset(&train, 0, sizeof(train));
              if( fun_mkdir_by_sql(conn, &info)){
                  printf("创建成功\n");
                  memset(info.command_str_return, 0, sizeof(info.command_str_return));
                  strcpy(train.buf, "创建成功");
                  sprintf(info.command_str_return, "%s", train.buf);
                  sendInfoStruct(net_fd, &info);
              }else{
                  printf("创建失败，已有同名文件");
                  memset(info.command_str_return, 0, sizeof(info.command_str_return));
                  strcpy(train.buf, "创建失败，已有同名文件");
                  sprintf(info.command_str_return, "%s", train.buf);
                  sendInfoStruct(net_fd, &info);

              }
              break;
            case 5:
                 LOGI("user:%s, ls\n", info.username);
                 memset(&train, 0, sizeof(train));
              if( fun_rm_by_sql(conn, &info)){
                  printf("删除成功\n");
                  memset(info.command_str_return, 0, sizeof(info.command_str_return));
                  strcpy(train.buf, "删除成功");
                  sprintf(info.command_str_return, "%s", train.buf);
                  sendInfoStruct(net_fd, &info);
              }else{
                  printf("删除失败，未找到文件");
                  memset(info.command_str_return, 0, sizeof(info.command_str_return));
                  strcpy(train.buf, "删除失败，未找到文件");
                  sprintf(info.command_str_return, "%s", train.buf);
                  sendInfoStruct(net_fd, &info);
              }
              break;
            case 6:
                memset(server_path, 0, sizeof(server_path));
                getparameter("root", server_path);
                recvFile(net_fd, server_path, conn, info);
                 printf("sonThread\n");
              break;
            case 7:
                memset(server_path, 0, sizeof(server_path));
                getparameter("root", server_path);
                sendFile(net_fd, server_path, conn, info);
              break;
              /*
            case 6:
                //puts
                //日志
                LOGI("user:%d, puts %s\n", user_id, command);
                
                recvFile(net_fd, info.path);
                //printf("after recvFile\n");
                break;
            case 7:
                //gets
                recv(net_fd, &train.len, sizeof(int), MSG_WAITALL);
                memset(name, 0, sizeof(name));
                recv(net_fd, name, train.len, MSG_WAITALL);

                //日志
                LOGI("user:%d, gets %s\n", user_id, command);
                sendFile(net_fd, info.path, name);
                break;
            default:
                send(net_fd, "error command!\n", 15, MSG_NOSIGNAL);*/
            }

        }

        close(net_fd);
    }
    return NULL;
}


